package com.hangxin.system.web;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import com.hangxin.commons.util.SysUtil;
import com.hangxin.system.util.GlobalUtil;

public class downloadServlet extends HttpServlet
{

  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, FileUploadException
  {
	  String path = request.getParameter("path");
	  PrintWriter out = response.getWriter();
		response.setCharacterEncoding("GBK");
		try {
			//String path = java.net.URLDecoder.decode((request.getParameter("path")==null?"":request.getParameter("path")), "gbk");
			// 清空response
			response.reset();
			String phsical = GlobalUtil.getProperty("path");
			String  filePath = path;
          File file = new File(filePath); 
          String filename = file.getName();
          String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
          String mime = SysUtil.setMime(ext);
          // 以流的形式下载文件。
          InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
          String name = filename.substring(0,filename.length()-4).length()>33?filename.substring(0,filename.length()-4).substring(0,33):filename.substring(0,filename.length()-4);
          response.addHeader("Content-Disposition", "attachment;filename="+new String(name.getBytes("gbk"),"iso-8859-1")+"."+ext);
          response.addHeader("Content-Length", "" + file.length());
          OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
          response.setContentType("application/octet-stream");
          byte[] b = new byte[100];
          int forch = fis.available()/100;
          for(int i=0;i<=forch;i++){
          	  fis.read(b,0,100);
          	  toClient.write(b);
          }
          fis.close();
          toClient.flush();
          toClient.close();
      } catch (IOException ex) {
      	out.print("<script>");
      	out.print("alert('"+new String("您要下载的文件不存在".getBytes("gbk"),"iso-8859-1")+"');window.history.back();window.close();");
      	out.print("</script>");
      }
	}

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    try {
		processRequest(request, response);
	} catch (FileUploadException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
  }

  protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    try {
		processRequest(request, response);
	} catch (FileUploadException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
  }

  public String getServletInfo()
  {
    return "Short description";
  }
}